﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Classes;
using DataAccess;

namespace Webmau.Controls
{
    public partial class BaiVieta : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControls();
            }
        }

        private void PopulateControls()
        {
            int howManyPages = 0;
            string page = Request.QueryString["Page"] ?? "1";

            string firstPageUrl = "";
            string pagerFormat = "";

            if (txtTim.Text != "")
            {
                dlBaiViet.DataSource = BaiViet.TimTheoNguoiDungViet(txtTim.Text, page, out howManyPages);
                dlBaiViet.DataBind();

                firstPageUrl = Link.ToDanhMucBaiBiet();
                pagerFormat = Link.ToDanhMucBaiBiet("{0}");
            }
            else
            {
                if (txtTim.Text == "")
                {

                    dlBaiViet.DataSource = BaiViet.LayDanhSach(page, out howManyPages);
                    dlBaiViet.DataBind();
                    firstPageUrl = Link.ToDanhMucBaiBiet();
                    pagerFormat = Link.ToDanhMucBaiBiet("{0}");

                }
            }
            pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            pagerButtom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }
        protected void btnTim_Click(object sender, EventArgs e)
        {
            int howManyPages = 0;
            string page = Request.QueryString["Page"] ?? "1";

            string firstPageUrl = "";
            string pagerFormat = "";
            dlBaiViet.DataSource = BaiViet.TimTheoNguoiDungViet(txtTim.Text, page, out howManyPages);
            dlBaiViet.DataBind();

            firstPageUrl = Link.ToDanhMucBaiBiet();
            pagerFormat = Link.ToDanhMucBaiBiet("{0}");
            pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            pagerButtom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }
    }
}
